﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：马毅
    //
    // 文件名：ProductList.cs
    // 文件功能描述：产品列表
    // 创建标识：马毅2009-05-12
    //
    // 修改标识： 
    // 修改描述： 
    //
    // 修改标识： 
    // 修改描述： 
    //
    // 审核人： 
    // 审核时间： 
    // 审核说明： 
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{
    public partial class ProductList : BasePage
    {
        public ProductList()
        { }
        IBLL_ProcurementBasic pbCtrl = Factory.BLL_ProcurementBasic();
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //绑定所有项目
                ProcurementProject procurementProject = new ProcurementProject();
                procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
                procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
                procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
                procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
                int temp;
                IList<ProcurementProject> projectList = ipb.GetProjectByWhere(procurementProject, 1, 0, out temp);
                ddlProject.DataSource = projectList;
                ddlProject.DataTextField = "ProjectName";
                ddlProject.DataValueField = "ProjectID";
                ddlProject.DataBind();
                ListItem item = new ListItem();
                item.Text = Constants.ALL_ITEM_TEXT;
                item.Value = Constants.ALL_ITEM_VALUE;
                ddlProject.Items.Insert(0, item);
                if (!string.IsNullOrEmpty(Request.QueryString["ProjectId"]))
                {
                    for (int i = 0; i < ddlProject.Items.Count; i++)
                    {
                        if (ddlProject.Items[i].Value == Request.QueryString["ProjectId"]) { ddlProject.Items[i].Selected = true; }
                    }
                    Bind();
                }
                else
                {
                    setBind();
                }
            }
        }
        /// <summary>
        /// 绑定从采购项目传过来的采购产品
        /// </summary>
        private void Bind()
        {
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.SortFields = "AddTime DESC";
            procureCatalog.ProjectID = Request.QueryString["ProjectId"];
            procureCatalog.CompanyID_TB = CurrentAccount.GetUser().Org.OrgID;
            //调用方法，执行查询，分页显示产品信息
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhere(procureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwProduceCatalog.DataSource = procureCatalogList;
            gvwProduceCatalog.DataBind();
        }


        private void setBind()
        {
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            procureCatalog.Material = txtMaterial.Text.Trim();
            procureCatalog.Outlookc = txtOutLook.Text.Trim();

            procureCatalog.ProjectID = ddlProject.SelectedItem.Value;
            procureCatalog.Unit = txtUnit.Text.Trim();
            procureCatalog.CompanyID_TB = CurrentAccount.GetUser().Org.OrgID;
            procureCatalog.CompanyName_TB = txtCompany_TB.Text;
            procureCatalog.SortFields = "AddTime DESC";

            if (!string.IsNullOrEmpty(txtAddTimeFrom.Text.Trim()))
            {
                procureCatalog.AddTimeFrom = Convert.ToDateTime(txtAddTimeFrom.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtAddTimeTo.Text.Trim()))
            {
                procureCatalog.AddTimeTo = Convert.ToDateTime(txtAddTimeTo.Text.Trim());
            }

            //调用方法，执行查询，分页显示产品信息
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
            try
            {
                IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhere(procureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                AspNetPager1.RecordCount = totalCount;
                gvwProduceCatalog.DataSource = procureCatalogList;
                gvwProduceCatalog.DataBind();
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            setBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }
        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAbandon_Click(object sender, EventArgs e)
        {
            string ids = Request["checkbox1"];

            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    Model.View.ProcureCatalog tmpProcureCatalog = pbCtrl.GetProcureCatalogByID(Item);
                    if (Convert.ToBoolean(tmpProcureCatalog.IsUsing))
                    {
                        ProcureCatalog aProcureCatalog = new ProcureCatalog();
                        aProcureCatalog.IsUsing = false;
                        aProcureCatalog.ProcureCatalogID = tmpProcureCatalog.ProcureCatalogID;
                        int nSuccess = pbCtrl.UpdateProcureCatalog_IsUsing(aProcureCatalog);
                        if (nSuccess == 0)
                        {
                            //PageError("数据操作失败！", "");
                            HZ.Web.WebUtil.Alert("数据操作失败！", this);
                        }
                    }
                }
            }
            setBind();
        }

        /// <summary>
        /// 恢复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecover_Click(object sender, EventArgs e)
        {
            string ids = Request["checkbox1"];

            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    Model.View.ProcureCatalog tmpProcureCatalog = pbCtrl.GetProcureCatalogByID(Item);
                    if (!Convert.ToBoolean(tmpProcureCatalog.IsUsing))
                    {
                        ProcureCatalog aProcureCatalog = new ProcureCatalog();
                        aProcureCatalog.IsUsing = true;
                        aProcureCatalog.ProcureCatalogID = tmpProcureCatalog.ProcureCatalogID;
                        int nSuccess = pbCtrl.UpdateProcureCatalog_IsUsing(aProcureCatalog);
                        if (nSuccess == 0)
                        {
                            //PageError("数据操作失败！", "");
                            HZ.Web.WebUtil.Alert("数据操作失败！", this);
                        }
                    }
                }
            }
            setBind();
        }
        /// <summary>
        /// 简单查询与高级查询间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtAddTimeFrom.Text = "";
                txtAddTimeTo.Text = "";
                txtCompany_TB.Text = "";
                txtMaterial.Text = "";
                txtGoodsName.Text = "";
                txtProduceCompany_SC.Text = "";
                txtUnit.Text = "";
                ddlProject.SelectedIndex = -1;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click1(object sender, EventArgs e)
        {
            setBind();
        }



    }
}
